import aepApiRequest from '../utils/aepApiRequest';
import {cozeApiRequest} from '../utils/aepApiRequest';
import uploadRequest from '../utils/uploadRequest';

export interface IDestructionAccountData {
  type: 1 | 2 | 3; //1,手机,2,邮箱,3,不填(给小程序使用)
  code?: string;
}

export interface IUploadImageData {
  uri: string;
  name: string;
}

export interface IWXLoginParam {
  code: string;
  encryptedData?: string,
  iv?: string
}

export function wxLogin(params: IWXLoginParam) {
  const { code } = params;
  const url = `app/applets/thirdLogin?code=${code}`;
  const option = {
    method: 'POST',
    body: params,
  };
  return cozeApiRequest(url, option, false);
}

// 获取用户信息
export function accountInfo(token?: string) {
  const url = 'app/smart_home/users/detail';
  const option = {
    method: 'Get',
  };
  if (token) {
    option.headers = { Authorization: token };
  }
  return aepApiRequest(url, option, true);
}
// 除手机，邮箱信息 ，修改用户信息
export function changeInfo(data: object) {
  const url = 'app/smartHome/users';
  const option = {
    method: 'PUT',
    body: data,
  };
  return aepApiRequest(url, option, true);
}
export function updateUserInfo(data: object, token?: string) {
  const path = 'app/smartHome/users/mobile';
  const option = {
    method: 'PUT',
    body: data,
  };
  if (token) {
    option.headers = { Authorization: token };
  }
  return aepApiRequest(path, option, true);
}
export function updateEmailInfo(data: object) {
  const path = 'app/smartHome/users/mail';
  return aepApiRequest(path, {
    method: 'PUT',
    body: data,
  });
}
export function destructionAccount(data: IDestructionAccountData) {
  const path = 'app/smartHome/users';
  return aepApiRequest(path, {
    method: 'DELETE',
    body: data,
  });
}
export function uploadImage(data: IUploadImageData) {
  const options = {
    method: 'POST',
    ...data,
  };
  return uploadRequest(options);
}

export async function modifyPwd(params: {
  newPassword: string;
  oldPassword: string;
}) {
  return aepApiRequest('app/smart_home/pwd', {
    method: 'POST',
    body: params,
  });
}
